package cn.rubintry.bug.hunting.activity

import android.util.Base64
import android.view.View
import androidx.activity.viewModels
import cn.rubintry.bug.common.base.BaseActivity
import cn.rubintry.bug.hunting.R
import cn.rubintry.bug.hunting.databinding.ActivityLoginBinding
import cn.rubintry.bug.hunting.common.event.LoginEvent
import cn.rubintry.bug.hunting.common.http.viewmodel.LoginViewModel
import cn.rubintry.bug.hunting.common.manager.UserManager
import cn.rubintry.bug.hunting.common.password.SafePasswordTransformationMethod

import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import org.greenrobot.eventbus.EventBus

class LoginActivity : BaseActivity<ActivityLoginBinding>(), View.OnClickListener {

    private var verifyUUid: String = ""
    private val loginViewModel: LoginViewModel by viewModels<LoginViewModel>()

    override fun bindingClazz(): Class<ActivityLoginBinding> {
        return ActivityLoginBinding::class.java
    }

    override fun getActivityParams() {

    }

    override fun initView() {
        binding?.imgBack?.setOnClickListener(this)
        binding?.imgLogo?.let {
            Glide.with(this)
                .load(R.mipmap.mlogo)
                .into(it)
        }
        binding?.etPassword?.transformationMethod = SafePasswordTransformationMethod()
        binding?.tvLogin?.setOnClickListener(this)
        binding?.imgVerifyCode?.setOnClickListener(this)
    }

    override fun onStart() {
        super.onStart()
        loginViewModel.getVerifyCode()
    }

    override fun observeData() {
        loginViewModel.verifyData.observe(this) { data ->
            if(data.code == "0"){
                data.data.verifyCode?.let { base64 ->
                    Glide.with(this@LoginActivity)
                        .load(Base64.decode(base64 , Base64.DEFAULT))
                        .into(binding?.imgVerifyCode!!)
                    this@LoginActivity.verifyUUid = data.data.uuid
                }
            }else{
                ToastUtils.showShort("验证码获取失败：" + data.message)
            }
        }

        loginViewModel.loginData.observe(this){
            if(it.code == "0"){
                ToastUtils.showShort("登录成功")
                UserManager.getInstance().save(it.data)
                EventBus.getDefault().post(LoginEvent(true , ""))
                finish()
            }else{
                EventBus.getDefault().post(LoginEvent(false , it.message))
                ToastUtils.showShort("登录失败：" + it.message)
            }
        }
    }

    override fun onNetWorkError(msg: String?) {

    }

    override fun onClick(v: View?) {
        when(v){
            binding?.imgBack -> {
                finish()
            }

            binding?.imgVerifyCode -> {
                loginViewModel.getVerifyCode()
            }

            binding?.tvLogin -> {
                if(binding?.etUserName?.text?.isEmpty() == true){
                    ToastUtils.showShort("请输入用户名")
                    return
                }
                if(binding?.etPassword?.text?.isEmpty() == true){
                    ToastUtils.showShort("请输入密码")
                    return
                }
                if(binding?.etVerifyCode?.text?.isEmpty() == true){
                    ToastUtils.showShort("请输入验证码")
                    return
                }
                loginViewModel.login(binding?.etUserName?.text?.toString() ?: "", binding?.etPassword?.text?.toString() ?: "" , verifyUUid , binding?.etVerifyCode?.text?.toString() ?: "")
            }
        }
    }
}